package com.open4cn.simulator.core.util;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-08-07 15:45
 */
public final class TranscodeUtil {
    private TranscodeUtil() {
    }


    /**
     * 16进制转Ascii字符串
     *
     * @param hex
     * @return
     */
    public static String hexToString(String hex) {
        StringBuilder builder = new StringBuilder();
        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {
            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            builder.append((char) decimal);
        }

        return builder.toString();
    }


    /**
     * 数值转16进制
     *
     * @param num
     * @return
     */
    public static String numTohexString(int num) {
        String numStr = Integer.toHexString(num);
        return StrFillUtil.supplementZeroStr(numStr, 2);
    }

    /**
     * 数值转16进制
     *
     * @param num
     * @return
     */
    public static String numTohexString(Long num) {
        String numStr = Long.toHexString(num);
        return StrFillUtil.supplementZeroStr(numStr, 2);
    }

}
